package org.example.panel.studentpanel;
import javax.swing.*;
import java.awt.event.*;
import org.example.operation.*;

/**
 * @author lenovo
 */

public class AddStudentPanel extends JPanel {
    public AddStudentPanel(){
        //组件准备
        JTextField name = new JTextField(15);
        JTextField gender = new JTextField(15);
        JTextField birthday = new JTextField(15);
        JButton addStudent = new JButton("添加学生");
        Box nameBox = Box.createHorizontalBox();
        Box genderBox = Box.createHorizontalBox();
        Box birthdayBox = Box.createHorizontalBox();
        Box box = Box.createVerticalBox();
        //注册监视器
        addStudent.addActionListener((ActionEvent e)->{
            if(name.getText().isEmpty()||gender.getText().isEmpty() ||
                    birthday.getText().isEmpty()) {
                JOptionPane.showMessageDialog(null,
                        "信息不能为空", null, JOptionPane.WARNING_MESSAGE);
            }else if(name.getText().matches(".*\\s+.*") ||
                    gender.getText().matches(".*\\s+.*") ||
                    birthday.getText().matches(".*\\s+.*")){
                JOptionPane.showMessageDialog(null,
                        "信息不能有空格", null, JOptionPane.WARNING_MESSAGE);
            }else{
                String num = StudentOperation.addStudent(name.getText(),gender.getText(),
                        birthday.getText());
                if(num == null){
                    JOptionPane.showMessageDialog(null,
                            "学生数超出800000上限", null, JOptionPane.WARNING_MESSAGE);
                }else{
                    JOptionPane.showMessageDialog(null,
                            "添加学生成功\n学号：" + num, null,
                            JOptionPane.WARNING_MESSAGE);
                }
            }
        });
        //添加组件
        nameBox.add(new JLabel("姓名： "));
        nameBox.add(name);
        genderBox.add(new JLabel("性别： "));
        genderBox.add(gender);
        birthdayBox.add(new JLabel("出生日期： "));
        birthdayBox.add(birthday);
        box.add(nameBox);
        box.add(Box.createVerticalStrut(5));
        box.add(genderBox);
        box.add(Box.createVerticalStrut(5));
        box.add(birthdayBox);
        box.add(Box.createVerticalStrut(5));
        box.add(addStudent);
        add(box);
    }
}
